﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClassLibrary.DAO;
using ClassLibrary.Entity;

namespace mprsc
{
    public partial class PhoneBrand_Model : Form
    {
        TblPhoneBranDAO phoneBrandDaO = new TblPhoneBranDAO(Global.CONNECTION_STRING);
        TblModelDAO modelDAO = new TblModelDAO(Global.CONNECTION_STRING);

        public PhoneBrand_Model()
        {
            InitializeComponent();
        }

        private bool Check_BrandExists()
        {
            bool bExists = false;
            try
            {
                bExists = phoneBrandDaO.check_phonebrand(txtBrandName.Text);
                System.Threading.Thread.Sleep(3000);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return bExists;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtBrandName.Text == "")
            {
                this.validationProvider1.ValidationMessages(!this.validationProvider1.Validate());
            }
            else
            {
                bool flag = false;
                try
                {
                    TblPhoneBrand phoneBrand = new TblPhoneBrand();
                    //string id = grdPhoneBrand.CurrentRow.Cells["IDBrand"].Value.ToString();
                    phoneBrand.PhoneBrandName = txtBrandName.Text;

                    if (lbIDBrand.Text == null || lbIDBrand.Text == "")
                    {
                        if (Check_BrandExists())
                        {
                            MessageBox.Show(" Phone Brand already existed.");
                        }
                        else
                        {
                            phoneBrand.IDPhoneBrand = 0;
                            phoneBrandDaO.InUpPhoneBrand(phoneBrand, 1);
                        }
                    }
                    else
                    {
                       // int idBrand = Global.ConvertToInt(id);
                        TblPhoneBrand phone = new TblPhoneBrand();
                        phone.IDPhoneBrand = Global.ConvertToInt(lbIDBrand.Text) ;
                        phone.PhoneBrandName = txtBrandName.Text;
                        phoneBrandDaO.InUpPhoneBrand(phone, 2);
                    }
                    flag = true;
                    loadBrand();
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                if (flag)
                {
                    loadBrand();
                }
            }
        }

        private void PhoneBrand_Model_Load(object sender, EventArgs e)
        {
            loadBrand();
            this.CenterToScreen();
            LoadModel();
            LoadCombox_Brand();
        }
        
        private void loadBrand()
        {
            grdPhoneBrand.DataSource = phoneBrandDaO.GetAll();
          
            //Add 1 combobox in datagridview
            /*DataGridViewComboBoxColumn ComboBoxTabel = new DataGridViewComboBoxColumn();
            ComboBoxTabel.Name = "Model";
            ComboBoxTabel.AutoComplete = true;
            ComboBoxTabel.DataSource = modelDAO.GetModelByIdBrand(Global.ConvertToInt(grdPhoneBrand.CurrentRow.Cells["IDBrand"].Value.ToString()));
            ComboBoxTabel.DisplayMember = "Model";
            ComboBoxTabel.ValueMember = "IDModel";
            grdPhoneBrand.Columns.Add(ComboBoxTabel); */
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
           
            bool flag = false;
            if (MessageBox.Show("Do you want to delete this Brand ?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {
                    string idBrand = grdPhoneBrand.CurrentRow.Cells["IDBrand"].Value.ToString();
                    TblPhoneBrand product = new TblPhoneBrand();
                    DataTable dt = phoneBrandDaO.GetIDBrand(Global.ConvertToInt(idBrand));
                    phoneBrandDaO.Delete(Global.ConvertToInt(idBrand));
                    flag = true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            if (flag)
            {
                loadBrand();
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
           
            string idBrand = grdPhoneBrand.CurrentRow.Cells["IDBrand"].Value.ToString();
            if (idBrand == "")
            {
                MessageBox.Show("IDBrand not null");
            }
            else
            {
                DataTable dt = phoneBrandDaO.GetIDBrand(Global.ConvertToInt(idBrand));
                foreach (DataRow dr in dt.Rows)
                {
                    txtBrandName.Text = dr["BrandName"].ToString();
                    //lbIDModel.Text = dr["IDModel"].ToString();
                }
            }
        }

        private void grdPhoneBrand_Click(object sender, EventArgs e)
        {
            //Column1.DataSource = modelDAO.GetModelByIdBrand(Global.ConvertToInt(grdPhoneBrand.CurrentRow.Cells["IDBrand"].Value.ToString()));
            //Column1.DisplayMember = "Model";
            //Column1.ValueMember = "IDModel";
            //Column1.ReadOnly.ToString();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtBrandName.Text = string.Empty;
            lbIDBrand.Text = string.Empty;
        }    

        //Model
        private bool CheckModel()
        {
            bool bExists = false;
            try
            {

                bExists = modelDAO.check_model(txtModel.Text);
                System.Threading.Thread.Sleep(3000);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return bExists;
        }

        public void Detail_model()
        {

            string id = grdModel.CurrentRow.Cells["IDModel"].Value.ToString();
            if (id == null)
            {
            }
            else
            {
                DataTable dt = modelDAO.GetModelById(Convert.ToInt32(id));
                foreach (DataRow dr in dt.Rows)
                {
                    txtModel.Text = dr["Model"].ToString();
                    cbBrand.SelectedValue = dr["IDBrand"].ToString();
                    lbIDModel.Text = dr["IDModel"].ToString();
                }
            }
        }

        private void LoadModel()
        {
            grdModel.DataSource = modelDAO.GetModel();
        }
        private void LoadCombox_Brand()
        {
            cbBrand.DataSource = phoneBrandDaO.GetAll();
            cbBrand.DisplayMember = "BrandName";
            cbBrand.ValueMember = "IDBrand";
        }

        private void btnSaveModel_Click(object sender, EventArgs e)
        {
            if (txtModel.Text == "")
            {
                this.validationProvider1.ValidationMessages(!this.validationProvider1.Validate());
            }
            else
            {
                bool flag = false;
                try
                {
                    TblModel model = new TblModel();    
                    model.Model = txtModel.Text;
                    model.BrandID = cbBrand.SelectedValue.ToString();

                    if (lbIDModel.Text == null || lbIDModel.Text == "")
                    {
                        if (CheckModel())
                        {
                            MessageBox.Show("Model already existed.");
                        }
                        else
                        {
                            model.IDModel = 0;
                            modelDAO.Model_InUp(model, 1);
                        }
                    }
                    else
                    {
                        //int idModel = Global.ConvertToInt(id);
                        TblModel model1 = new TblModel();
                        model1.BrandID = cbBrand.SelectedValue.ToString();
                        model1.IDModel = Global.ConvertToInt(lbIDModel.Text);
                        model1.Model = txtModel.Text;
                        modelDAO.Model_InUp(model1, 2);
                    }
                    flag = true;
                    LoadModel();
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                if (flag)
                {
                    LoadModel();
                    txtModel.Text = string.Empty;
                }
            }
        }

        private void btnCancelModel_Click(object sender, EventArgs e)
        {
            txtModel.Text = string.Empty;
            lbIDModel.Text = string.Empty;
        }

        private void btnDetailModel_Click(object sender, EventArgs e)
        {
            Detail_model();
        }

        private void btnDeleteModel_Click(object sender, EventArgs e)
        {
           
            bool flag = false;
            try
            {

                TblModel model = new TblModel();
                string IDDelete = grdModel.CurrentRow.Cells["IDModel"].Value.ToString();
                model.IDModel = Global.ConvertToInt(IDDelete);
                modelDAO.Delete(Global.ConvertToInt(IDDelete));
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (flag)
            {
                LoadModel();
            }
        }

        private void grdModel_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex;
            txtModel.Text = grdModel[1, i].Value.ToString();
            cbBrand.Text = grdModel[3, i].Value.ToString();
            lbIDModel.Text = grdModel[0, i].Value.ToString();
        }

        private void grdPhoneBrand_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex;
            lbIDBrand.Text = grdPhoneBrand[0, i].Value.ToString();
            txtBrandName.Text = grdPhoneBrand[1, i].Value.ToString();
        }
    }
}
